using System;
using System.Collections.Generic;
using System.Linq;
using Budgeteer.Services;
using Budgeteer.Services.Common;
using Budgeteer.Services.DataProviders;
using Budgeteer.Services.Domain;
using Test.UnitTests.Budgeteer.Services.DataProviders;
using Xunit;

namespace Test.UnitTests.Budgeteer.Services
{
    public class ChargeServiceFacts
    {
        public class ParseTransactionFiles
        {
            [Fact]
            public void ShouldParseDataFilesSuccessfully()
            {
                string dataFolder = Config.TestDataFolder;

                ConfigService config = new ConfigService();
                ChargeService service = new ChargeService(new ChargeDataProvider(Config.ConnectionString),config);

                IList<ChargeDto> transactions = service.ParseTransactionFiles(dataFolder,(c) => "COFFEE");

                Assert.True(transactions.Count > 0);
                ChargeDto charge = transactions[0];
                Assert.Equal(new DateTime(2008,8,12),charge.Time);
                Assert.Equal("STARBUCKS USA 00033001 MIAMI FL",charge.Name);
                Assert.Equal(7.3M,charge.Amount);
                Assert.Equal("COFFEE",charge.Category);
                Assert.Equal("Visa",charge.Account);
            }
        }

        public class ImportTransactionFiles
        {
            [Fact]
            public void ShouldImportTransactionDataSuccessfully()
            {
                ConfigService config = new ConfigService();
                StubChargeDataProvider dataProvider = new StubChargeDataProvider();
                StubChargeService service = new StubChargeService(dataProvider,config);

                Func<string, string> getCatByCharge = c => "";

                service.ParseTransactionFiles_Results = new List<ChargeDto> {
                    new ChargeDto {
                        Time = DateTime.Parse("08/12/2008"),
                        Name = "STARBUCKS USA MIAMI FL",
                        Amount = 7.3M,
                        Category = "COFFEE",
                        Account = "Visa"
                    }
                };

                service.ImportTransactionData(getCatByCharge);

                Assert.True(dataProvider.InsertTransactionData_Called);
                Assert.Equal("STARBUCKS USA MIAMI FL",dataProvider.InsertTransactionData_Charge.Name);
            }
        }
    }

    public class StubChargeService : ChargeService
    {
        public IList<ChargeDto> ParseTransactionFiles_Results;
        public string ParseTransactionFiles_DataFolder;
        public Func<string, string> ParseTransactionFiles_GetChargeCategory;

        public StubChargeService(ChargeDataProvider chargeDataProvider,ConfigService config)
            : base(chargeDataProvider,config)
        {
        }

        public override IList<ChargeDto> ParseTransactionFiles(string dataFolder,Func<string,string> getChargeCategory)
        {
            ParseTransactionFiles_DataFolder = dataFolder;
            ParseTransactionFiles_GetChargeCategory = getChargeCategory;

            return ParseTransactionFiles_Results;
        }
    }
}